package com.alogic.jsoup;
import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;

/**
 * xscript插件
 *
 * @since 1.6.13.13 [20200904 duanyy] <br>
 */
public class Plugins extends Segment {

    public Plugins(String tag, Logiclet p) {
        super(tag, p);
        registerModule("wl-add-tag",AddTags.class);
        registerModule("wl-add-attr",AddAttrs.class);
        registerModule("wl-add-ext-attr",AddExtAttr.class);
    }

    /**
     * 操作基类
     */
    public static abstract class Operation extends AbstractLogiclet {
        /**
         * 上下文id
         */
        protected String pid = "$whitelist";

        public Operation(String tag, Logiclet p) {
            super(tag, p);
        }

        @Override
        public  void configure(Properties p){
            super.configure(p);
            pid = PropertiesConstants.getString(p,"pid",pid,true);
        }

        @Override
        protected void onExecute(XsObject root, XsObject current, LogicletContext ctx,
                                 ExecuteWatcher watcher) {
            WhiteList whitelist = ctx.getObject(pid);
            if (whitelist != null){
                onExecute(whitelist,root,current,ctx,watcher);
            }else{
                log(String.format("[%s]Whitelist object is null",this.getXmlTag()),LOG_WARNING,ctx);
            }
        }

        protected abstract void onExecute(WhiteList whitelist, XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher);
    }

    /**
     * 添加tag
     */
    public static class AddTags extends Operation {
        protected String $tag;
        protected String delimiter = ",";

        public AddTags(String tag, Logiclet p) {
            super(tag, p);
        }

        @Override
        public void configure(Properties p) {
            super.configure(p);
            $tag = PropertiesConstants.getRaw(p, "tag", $tag);
            delimiter = PropertiesConstants.getString(p, "delimiter", delimiter);
        }

        @Override
        protected void onExecute(WhiteList whitelist, XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
            String tags = PropertiesConstants.transform(ctx, $tag, "");
            if (StringUtils.isNotEmpty(tags)) {
                whitelist.addTags(tags.split(delimiter));
            }
        }
    }

    /**
     * 添加tag
     */
    public static class AddAttrs extends Operation {
        protected String $tag = null;
        protected String $attr = null;
        protected String delimiter = ",";

        public AddAttrs(String tag, Logiclet p) {
            super(tag, p);
        }

        @Override
        public void configure(Properties p){
            super.configure(p);

            $tag = PropertiesConstants.getRaw(p,"tag",$tag);
            $attr = PropertiesConstants.getRaw(p,"attr",$attr);
            delimiter = PropertiesConstants.getString(p,"delimiter",delimiter);
        }

        @Override
        protected void onExecute(WhiteList whitelist, XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
            String tag = PropertiesConstants.transform(ctx,$tag,"");
            if (StringUtils.isNotEmpty(tag)){
                String attr = PropertiesConstants.transform(ctx,$attr,"");
                whitelist.addTagAttrs(tag,attr.split(delimiter));
            }
        }
    }

    /**
     * 添加tag
     */
    public static class AddExtAttr extends Operation {
        protected String $tag = null;
        protected String $attr = null;
        protected String $value = null;

        public AddExtAttr(String tag, Logiclet p) {
            super(tag, p);
        }

        @Override
        public void configure(Properties p){
            super.configure(p);

            $tag = PropertiesConstants.getRaw(p,"tag",$tag);
            $attr = PropertiesConstants.getRaw(p,"attr",$attr);
            $value = PropertiesConstants.getRaw(p,"value",$value);
        }

        @Override
        protected void onExecute(WhiteList whitelist, XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
            String tag = PropertiesConstants.transform(ctx,$tag,"");
            if (StringUtils.isNotEmpty(tag)){
                String attr = PropertiesConstants.transform(ctx,$attr,"");
                if (StringUtils.isNotEmpty(attr) && StringUtils.isNotEmpty($value)) {
                    whitelist.addTagExtendAttr(tag,attr,$value);
                }
            }
        }
    }
}